<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/23
 * Time: 14:04
 */

namespace app\controllers;

use app\models\Express;
use yii;

class ExpressController extends ApiBaseController
{
    public function actionUpdate()
    {
        $model = new Express();
        $id = yii::$app->request->post('id');
        $info = $model->one(['id' => $id]);
        //校验锁定状态
        if ($info['lock'] == 1) {
            return json_encode(['code' => 0, 'msg' => '已被锁定无法修改']);
        }
        if (is_numeric($id)) {
            $column = yii::$app->request->post('column');
            $value = trim(yii::$app->request->post('value'));

            $data = [];
            $data['last_modify'] = $this->user['job_number'];
            $data['last_modify_time'] = time();
            $data[$column] = $value;
            if ($model->save($data, ['id' => $id])) {
                //更新拆分项金钱是否一致
                $info = $model->one(['id' => $id]);
                $model->validateExpressMoney($info['pid']);
                $output = ['code' => 1, 'msg' => '更新成功'];
            } else {
                $output = ['code' => 1, 'msg' => '更新失败,请重试'];
            }
            echo json_encode($output);
        }
    }


    /**
     * 快递order锁定
     */
    public function actionLock()
    {
        if (yii::$app->request->isPost) {
            $id = yii::$app->request->post('id');
            $lock = yii::$app->request->post('lock');
            $re = yii::$app->db->createCommand()->update(yii::$app->db->tablePrefix . 'express', ['lock' => $lock], ['id' => $id])->execute();
            if ($re) {
                echo json_encode(['code' => 1, 'msg' => '修改成功']);
            } else {
                echo json_encode(['code' => 0, 'msg' => '修改失败，请重试']);
            }
        }
    }

    public function actionSplitExpress()
    {
        if (!yii::$app->request->isPost) {
            return json_encode(['code' => 0, 'msg' => 'not allowed']);
        }

        $id = yii::$app->request->post('id');
        $invoice = yii::$app->request->post('invoice');
        $fee = yii::$app->request->post('fee');
        if (!$id) {
            return \json_encode(['code' => 0, 'msg' => '参数不全']);
        }
        //先复制一条记录
        $model = new Express();
        $info = $model->one(['id' => $id]);
        //拆分项无法拆分
        if ($info['pid'] != $info['id']) {
            return json_encode(['code' => 0, 'msg' => '该条记录是拆分项，无法继续拆分']);
        }
        $info['pid'] = $info['id'];
        unset($info['id']);
        $info['invoice_order'] = $invoice;
        $info['fee'] = $fee;
        $info['split_status'] = 2;
        $info['last_modify_time'] = time();
        $info['last_modify'] = $this->user['job_number'];
        $re = $model->insert($info);
        $model->validateExpressMoney($info['pid']);
        if ($re) {
            echo json_encode(['code' => 1, 'msg' => '添加成功']);
        } else {
            echo json_encode(['code' => 0, 'msg' => '添加失败']);
        }
    }

}